package cn.shop.dys.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.shop.dys.bean.UserInfo;
import cn.shop.dys.db.UserInfoDBHelper;
import cn.shop.dys.db.dbinfo.UserInfoDBInfo;

/**
 * 用户dao
 * Created by jian on 2016/4/19.
 */
public class UserInfoDao {
    private UserInfoDBHelper helper;
    public UserInfoDao(Context context){
        helper=new UserInfoDBHelper(context);
    }
    public void addUserInfo(UserInfo userInfo){
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(UserInfoDBInfo.COLUMN_USERNAME,userInfo.getUsername());
        values.put(UserInfoDBInfo.COLUMN_PASSWORD,userInfo.getPassword());
        values.put(UserInfoDBInfo.COLUMN_BONUS,userInfo.getBonus());
        values.put(UserInfoDBInfo.COLUMN_LEVEL,userInfo.getLevel());
        values.put(UserInfoDBInfo.COLUMN_USERID,userInfo.getUserid());
        values.put(UserInfoDBInfo.COLUMN_ORDERCOUNT,userInfo.getOrderCount());
        values.put(UserInfoDBInfo.COLUMN_FAVORITESCOUNT,userInfo.getFavoritesCount());
        db.insert(UserInfoDBInfo.USER_INFO_TABLE_NAME,null,values);
        db.close();
    }
    public boolean isHaveUserInfoByUserId(String userid){
        boolean isHaveUserInfo=false;
        SQLiteDatabase db = helper.getReadableDatabase();
        if(db.isOpen()){
            Cursor cursor = db.query(UserInfoDBInfo.USER_INFO_TABLE_NAME, new String[]{"*"},
                    UserInfoDBInfo.COLUMN_USERID+"=?",
                    new String[]{userid}, null, null, null);
            if(cursor!=null&&cursor.getCount()>0){
                isHaveUserInfo=cursor.moveToNext();
            }
            cursor.close();
            db.close();
        }
        return isHaveUserInfo;
    }
    public UserInfo getUserInfo(String userid){
        UserInfo userinfo=null;
        SQLiteDatabase db = helper.getReadableDatabase();
        if(db.isOpen()){
            Cursor cursor = db.query(UserInfoDBInfo.USER_INFO_TABLE_NAME, new String[]{"*"},
                    UserInfoDBInfo.COLUMN_USERID+"=?",
                    new String[]{userid}, null, null, null);
            if(cursor!=null&&cursor.getCount()>0){
                userinfo=new UserInfo();
                while(cursor.moveToNext()){
                    userinfo.setId(cursor.getInt(0));
                    userinfo.setUsername(cursor.getString(1));
                    userinfo.setPassword(cursor.getString(2));
                    userinfo.setBonus(cursor.getInt(3));
                    userinfo.setLevel(cursor.getString(4));
                    userinfo.setUserid(cursor.getString(5));
                    userinfo.setOrderCount(cursor.getInt(6));
                    userinfo.setFavoritesCount(cursor.getInt(7));
                }
            }
            cursor.close();
            db.close();
        }
        return  userinfo;
          /* private int id;
    private String username;
    private String password;
    private int bonus;
    private String level;
    private String userid;
    private int orderCount;
    private int favoritesCount;
    private boolean logState;*/
    }
}
